package com.zhuanzhuan.hero.user.helper;

import com.bj58.youpin.common.utils.json.JsonUtils;
import com.bj58.zhuanzhuan.zzuser.contract.IZZUserService;
import com.bj58.zhuanzhuan.zzuser.entity.UserApiResult;
import com.bj58.zhuanzhuan.zzuser.entity.ZZUser;
import com.bj58.zhuanzhuan.zzuser.utils.AesUtil;
import com.zhuanzhuan.hero.biz.common.util.WxWarnTemplateUtil;
import com.zhuanzhuan.hero.user.consts.Consts;
import com.zhuanzhuan.hero.user.consts.NumConsts;
import com.zhuanzhuan.hero.user.enums.ExceptionMsgEnum;
import com.zhuanzhuan.hero.user.exception.BusinessException;
import com.zhuanzhuan.youpin.log.interceptor.LogContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.security.GeneralSecurityException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @ClassName ZzUserHelper
 * @Description
 * @Author hanjunjun
 * @Date 2024/6/17 20:55
 */
@Component
@Slf4j
public class ZzUserHelper {

    private final static int AES_SOURCE = 29;
    private final static String AES_KEY = "24d12ec3555f27f9e870bc32ce88c116";

    @Resource
    private IZZUserService zzUserService;

    public ZZUser getUserByUid(Long uid) {
        ZZUser zzUser;
        try {
            zzUser = zzUserService.getByUid(uid, LogContext.getLogStr());
        } catch (Exception e) {
            log.error("根据uid查询用户信息失败", e);
            WxWarnTemplateUtil.warnOutService(Consts.SERVICE_NAME, "scf接口调用失败");
            throw new BusinessException(ExceptionMsgEnum.BUSINESS_ERROR);
        }

        log.info("查询用户信息,result={}", JsonUtils.toJsonWithoutNull(zzUser));
        return zzUser;
    }

    public ZZUser getUserByMobile(String mobile) {
        ZZUser zzUser;
        try {
            log.info("getByMobile mobile={}", mobile);
            zzUser = zzUserService.getByMobile(mobile, LogContext.getLogStr());
            log.info("getByMobile, result={}", JsonUtils.toJsonWithoutNull(zzUser));
        } catch (Exception e) {
            log.error("根据mobile查询用户信息失败", e);
            WxWarnTemplateUtil.warnOutService(Consts.SERVICE_NAME, "scf接口调用失败");
            throw new BusinessException(ExceptionMsgEnum.BUSINESS_ERROR);
        }

        return zzUser;
    }

    /**
     * 根据uid查询明文手机号
     *
     * @param uid
     * @return java.lang.String
     * @author  hanjunjun
     * @date 2024/7/10
     **/
    public String getRealMobileByUid(Long uid) {
        Set<Long> set = new HashSet<>();
        set.add(uid);
        UserApiResult<List<ZZUser>> result;
        try {
            log.info("getUserInfoByUids4AesEncrpy uid={}", uid);
            result = zzUserService.getUserInfoByUids4AesEncrpy(LogContext.getLogStr(),
                set, AES_SOURCE);
            log.info("getUserInfoByUids4AesEncrpy result={}", JsonUtils.toJsonWithoutNull(result));
        } catch (Exception e) {
            log.error("getUserInfoByUids4AesEncrpy调用失败", e);
            WxWarnTemplateUtil.warnOutService(Consts.SERVICE_NAME, "scf接口调用失败");
            throw new BusinessException(ExceptionMsgEnum.BUSINESS_ERROR, e);
        }
        if (!result.isSuccess()) {
            log.error("getUserInfoByUids4AesEncrpy调用失败");
            WxWarnTemplateUtil.warnOutService(Consts.SERVICE_NAME, "scf接口调用失败");
            throw new BusinessException(ExceptionMsgEnum.BUSINESS_ERROR);
        }

        List<ZZUser> userList = result.getBody();
        if (CollectionUtils.isEmpty(userList)) {
            return null;
        }

        try {
            String realMobile = userList.get(NumConsts.ZERO).getRealMobile(AES_KEY);
            log.info("realMobile = {}", realMobile);
            return realMobile;
        } catch (Exception e) {
            log.error("解析明文手机号失败", e);
            throw new BusinessException(ExceptionMsgEnum.BUSINESS_ERROR, e);
        }
    }

    /**
     *  使用用户中台加密方式，对敏感信息进行加密
     * @param info 需要加密的数据
     * @return 加密后的字符串
     */
    public static String encrypt(String info) {
        log.info("AesEncrypt param={}", info);
        if (StringUtils.isBlank(info)) {
            return info;
        }
        try {
            return AesUtil.encrypt(AES_KEY, info);
        } catch (GeneralSecurityException | DecoderException e) {
            log.error("信息加密失败",e);
            throw new BusinessException(ExceptionMsgEnum.BUSINESS_ERROR, e);
        }
    }

    /**
     * 使用用户中台解密方式，对信息进行解密
     * @param info 需要解密的字符串
     * @return 解密后的数据
     */
    public static String decrypt(String info) {
        log.info("AesDecrypt param={}", info);
        if (StringUtils.isBlank(info)) {
            return info;
        }
        try {
            return AesUtil.decrypt(AES_KEY, info);
        } catch (GeneralSecurityException | DecoderException e) {
            log.error("信息解密失败",e);
            throw new BusinessException(ExceptionMsgEnum.BUSINESS_ERROR, e);
        }
    }

    public static void main(String []args){
        /*String info = "18822252873";
        String encrypt = ZzUserHelper.encrypt(info);
        System.out.println(encrypt);*/

        String encrypt = "7f6a62feb5e0f272171aa9bc288ffe55";
        System.out.println(ZzUserHelper.decrypt(encrypt));
    }

}
